package generator

// setTemplate Set类型的模板
const setTemplate = `package {{.Package}}

import (
	"fmt"

	"gitee.com/lovengame/kit/sea/redis"
	"gitee.com/lovengame/kit/sea/util"

	l4g "github.com/ivanabc/log4go"
)

const {{.Struct.DbStructName}}ProtoBufferSize = {{.BufferSize}}

func (client {{.Struct.ClientName}}) Get{{.Struct.Name}}() ([]{{.Struct.UniqueType}}, error) {

	strKeyName := RedisOpKeySet{{.Struct.DbStructName}}
	ptReply := client.Cmd("SMEMBERS", strKeyName)
	return client.Get{{.Struct.Name}}ByReply(ptReply)
}

func (client {{.Struct.ClientName}}) Get{{.Struct.Name}}MCall() {

	strKeyName := RedisOpKeySet{{.Struct.DbStructName}}
	client.Append("SMEMBERS", strKeyName)
}

func (client {{.Struct.ClientName}}) Get{{.Struct.Name}}ByReply(
	ptReply *redis.Reply) ([]{{.Struct.UniqueType}}, error) {

	bufs, err := ptReply.ListBytes()
	if err != nil {
		l4g.Errorf("RedisOp Get {{.Struct.Name}} Error %v", err)
		return nil, err
	}
	ids := make([]{{.Struct.UniqueType}}, 0, len(bufs))
	for _, buf := range bufs {

		neg := false
		if buf[0] == '-' {
			buf = buf[1:]
			neg = true
		}
		val, ok := util.ParseUint64(buf)
		if ok {
			var value {{.Struct.UniqueType}}
			if neg {
				value = -{{.Struct.UniqueType}}(val)
			} else {
				value = {{.Struct.UniqueType}}(val)
			}
			ids = append(ids, value)
		} else {
			l4g.Errorf("parse int64 error %s", string(buf))
			return nil, fmt.Errorf("parse int64 error %s", string(buf))
		}

	}
	return ids, nil
}

func (client {{.Struct.ClientName}}) AddSome{{.Struct.Name}}(
	data []{{.Struct.UniqueType}}) error {

	strKeyName := RedisOpKeySet{{.Struct.DbStructName}}
	ptReply := client.Cmd("SADD", strKeyName, data)
	if ptReply.Err != nil {
		l4g.Errorf("RedisOp Add {{.Struct.Name}} Error %v, Data %v", ptReply.Err, data)
		return ptReply.Err
	}
	return nil
}

func (client {{.Struct.ClientName}}) AddSome{{.Struct.Name}}MCall(
	data []{{.Struct.UniqueType}}) {

	strKeyName := RedisOpKeySet{{.Struct.DbStructName}}
	client.Append("SADD", strKeyName, data)
}

func (client {{.Struct.ClientName}}) RemSome{{.Struct.Name}}(
	data []{{.Struct.UniqueType}}) error {

	strKeyName := RedisOpKeySet{{.Struct.DbStructName}}
	ptReply := client.Cmd("SREM", strKeyName, data)
	if ptReply.Err != nil {
		l4g.Errorf("RedisOp Rem {{.Struct.Name}} Error %v, Data %v", ptReply.Err, data)
		return ptReply.Err
	}
	return nil
}

func (client {{.Struct.ClientName}}) RemSome{{.Struct.Name}}MCall(
	data []{{.Struct.UniqueType}}) {

	strKeyName := RedisOpKeySet{{.Struct.DbStructName}}
	client.Append("SREM", strKeyName, data)
}

func (client {{.Struct.ClientName}}) DelAll{{.Struct.Name}}() error {

	strKeyName := RedisOpKeySet{{.Struct.DbStructName}}
	ptReply := client.Cmd("DEL", strKeyName)
	if ptReply.Err != nil {
		l4g.Errorf("RedisOp Del {{.Struct.Name}} Error %v", ptReply.Err)
		return ptReply.Err
	}
	return nil
}

func (client {{.Struct.ClientName}}) DelAll{{.Struct.Name}}MCall() {

	strKeyName := RedisOpKeySet{{.Struct.DbStructName}}
	client.Append("DEL", strKeyName)
}
`

// setTemplate Set类型的模板
const setTemplateWithSuffix = `package {{.Package}}

import (
	"strconv"

	"gitee.com/lovengame/kit/sea/redis"

	l4g "github.com/ivanabc/log4go"
)

const {{.Struct.DbStructName}}ProtoBufferSize = {{.BufferSize}}

func (client {{.Struct.ClientName}}) Get{{.Struct.Name}}SK(
	sufKey {{.Struct.KeyType}}) ([]{{.Struct.UniqueType}}, error) {

	return client.Get{{.Struct.Name}}SKs(strconv.FormatUint(sufKey, 10))
}

func (client {{.Struct.ClientName}}) Get{{.Struct.Name}}MCallSK(
	sufKey {{.Struct.KeyType}}) {

	client.Get{{.Struct.Name}}MCallSKs(strconv.FormatUint(sufKey, 10))
}

func (client {{.Struct.ClientName}}) AddSome{{.Struct.Name}}SK(
	sufKey {{.Struct.KeyType}}, data []{{.Struct.UniqueType}}) error {

	return client.AddSome{{.Struct.Name}}SKs(strconv.FormatUint(sufKey, 10), data)
}

func (client {{.Struct.ClientName}}) AddSome{{.Struct.Name}}MCallSK(
	sufKey {{.Struct.KeyType}}, data []{{.Struct.UniqueType}}) {

	client.AddSome{{.Struct.Name}}MCallSKs(strconv.FormatUint(sufKey, 10), data)
}

func (client {{.Struct.ClientName}}) RemSome{{.Struct.Name}}SK(
	sufKey {{.Struct.KeyType}}, data []{{.Struct.UniqueType}}) error {

	return client.RemSome{{.Struct.Name}}SKs(strconv.FormatUint(sufKey, 10), data)
}

func (client {{.Struct.ClientName}}) RemSome{{.Struct.Name}}MCallSK(
	sufKey {{.Struct.KeyType}}, data []{{.Struct.UniqueType}}) {

	client.RemSome{{.Struct.Name}}MCallSKs(strconv.FormatUint(sufKey, 10), data)
}

func (client {{.Struct.ClientName}}) DelAll{{.Struct.Name}}SK(
	sufKey {{.Struct.KeyType}}) error {

	return client.DelAll{{.Struct.Name}}SKs(strconv.FormatUint(sufKey, 10))
}

func (client {{.Struct.ClientName}}) DelAll{{.Struct.Name}}MCallSK(
	sufKey {{.Struct.KeyType}}) {

	client.DelAll{{.Struct.Name}}MCallSKs(strconv.FormatUint(sufKey, 10))
}

//=========================以下是具体实现=========================

func (client {{.Struct.ClientName}}) Get{{.Struct.Name}}SKs(
	sufKey string) ([]{{.Struct.UniqueType}}, error) {

	strKeyName := RedisOpKeySet{{.Struct.DbStructName}} + sufKey
	ptReply := client.Cmd("SMEMBERS", strKeyName)
	return client.Get{{.Struct.Name}}ByReply(ptReply)
}

func (client {{.Struct.ClientName}}) Get{{.Struct.Name}}MCallSKs(
	sufKey string) {

	strKeyName := RedisOpKeySet{{.Struct.DbStructName}} + sufKey
	client.Append("SMEMBERS", strKeyName)
}

func (client {{.Struct.ClientName}}) Get{{.Struct.Name}}ByReply(
	ptReply *redis.Reply) ([]{{.Struct.UniqueType}}, error) {

	bufs, err := ptReply.ListBytes()
	if err != nil {
		l4g.Errorf("RedisOp Get {{.Struct.Name}} Error %v", err)
		return nil, err
	}
	ids := make([]{{.Struct.UniqueType}}, 0, len(bufs))
	for _, buf := range bufs {

		ids = append(ids, {{.Struct.UniqueType}}(buf))

	}
	return ids, nil
}

func (client {{.Struct.ClientName}}) AddSome{{.Struct.Name}}SKs(
	sufKey string, data []{{.Struct.UniqueType}}) error {

	strKeyName := RedisOpKeySet{{.Struct.DbStructName}} + sufKey
	ptReply := client.Cmd("SADD", strKeyName, data)
	if ptReply.Err != nil {
		l4g.Errorf("RedisOp Add {{.Struct.Name}} Error %v, Data %v", ptReply.Err, data)
		return ptReply.Err
	}
	return nil
}

func (client {{.Struct.ClientName}}) AddSome{{.Struct.Name}}MCallSKs(
	sufKey string, data []{{.Struct.UniqueType}}) {

	strKeyName := RedisOpKeySet{{.Struct.DbStructName}} + sufKey
	client.Append("SADD", strKeyName, data)
}

func (client {{.Struct.ClientName}}) RemSome{{.Struct.Name}}SKs(
	sufKey string, data []{{.Struct.UniqueType}}) error {

	strKeyName := RedisOpKeySet{{.Struct.DbStructName}} + sufKey
	ptReply := client.Cmd("SREM", strKeyName, data)
	if ptReply.Err != nil {
		l4g.Errorf("RedisOp Rem {{.Struct.Name}} Error %v, Data %v", ptReply.Err, data)
		return ptReply.Err
	}
	return nil
}

func (client {{.Struct.ClientName}}) RemSome{{.Struct.Name}}MCallSKs(
	sufKey string, data []{{.Struct.UniqueType}}) {

	strKeyName := RedisOpKeySet{{.Struct.DbStructName}} + sufKey
	client.Append("SREM", strKeyName, data)
}

func (client {{.Struct.ClientName}}) DelAll{{.Struct.Name}}SKs(
	sufKey string) error {

	strKeyName := RedisOpKeySet{{.Struct.DbStructName}} + sufKey
	ptReply := client.Cmd("DEL", strKeyName)
	if ptReply.Err != nil {
		l4g.Errorf("RedisOp Del {{.Struct.Name}} Error %v", ptReply.Err)
		return ptReply.Err
	}
	return nil
}

func (client {{.Struct.ClientName}}) DelAll{{.Struct.Name}}MCallSKs(
	sufKey string) {

	strKeyName := RedisOpKeySet{{.Struct.DbStructName}} + sufKey
	client.Append("DEL", strKeyName)
}
`
